<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/%E5%8D%9A%E5%AE%A2.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/%E5%8D%9A%E5%AE%A2.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/%E5%8D%9A%E5%AE%A2.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="docker," />





  <link rel="alternate" href="/atom.xml" title="Createsequence's Blog" type="application/atom+xml" />






<meta name="description" content="一、Docker常用命令 123#镜像名			版本标签			镜像id			 创建时间			镜像大小REPOSITORY          TAG                 IMAGE ID            CREATED             SIZEhello-world         latest              fce289e99eb9        6 mont">
<meta property="og:type" content="article">
<meta property="og:title" content="Docker基本操作">
<meta property="og:url" content="http://blog.xiajibagao.top/2019/09/02/docker/Docker%E5%9F%BA%E6%9C%AC%E6%93%8D%E4%BD%9C/index.html">
<meta property="og:site_name" content="Createsequence&#39;s Blog">
<meta property="og:description" content="一、Docker常用命令 123#镜像名			版本标签			镜像id			 创建时间			镜像大小REPOSITORY          TAG                 IMAGE ID            CREATED             SIZEhello-world         latest              fce289e99eb9        6 mont">
<meta property="og:locale">
<meta property="og:image" content="http://img.xiajibagao.top/docker查看镜像.PNG">
<meta property="og:image" content="http://img.xiajibagao.top/docker查看容器.PNG">
<meta property="article:published_time" content="2019-09-01T16:00:00.000Z">
<meta property="article:modified_time" content="2020-10-27T02:54:46.761Z">
<meta property="article:author" content="Createsequence">
<meta property="article:tag" content="docker">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://img.xiajibagao.top/docker查看镜像.PNG">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '',
    scheme: 'Gemini',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":true,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://blog.xiajibagao.top/2019/09/02/docker/Docker基本操作/"/>





  <title>Docker基本操作 | Createsequence's Blog</title>
  








<meta name="generator" content="Hexo 5.2.0"></head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Createsequence's Blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">一个努力前进的程序猿</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br />
            
            搜索
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocomplete="off"
             placeholder="搜索..." spellcheck="false"
             type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://blog.xiajibagao.top/2019/09/02/docker/Docker%E5%9F%BA%E6%9C%AC%E6%93%8D%E4%BD%9C/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/Createsequence.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Createsequence's Blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">Docker基本操作</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-09-02T00:00:00+08:00">
                2019-09-02
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/docker/" itemprop="url" rel="index">
                    <span itemprop="name">docker</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  3.4k
                </span>
              

              

              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h2 id="一-docker常用命令">一、Docker常用命令</h2>
<figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#镜像名			版本标签			镜像id			 创建时间			镜像大小</span></span><br><span class="line"><span class="string">REPOSITORY</span>          <span class="string">TAG</span>                 <span class="string">IMAGE</span> <span class="string">ID</span>            <span class="string">CREATED</span>             <span class="string">SIZE</span></span><br><span class="line"><span class="string">hello-world</span>         <span class="string">latest</span>              <span class="string">fce289e99eb9</span>        <span class="number">6</span> <span class="string">months</span> <span class="string">ago</span>        <span class="number">1.</span><span class="string">84kB</span></span><br></pre></td></tr></table></figure>
<figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">docker</span> <span class="string">version/info</span> <span class="comment">#查看版本/信息</span></span><br><span class="line"><span class="string">docker</span> <span class="string">--help</span> <span class="comment">#查看说明文档</span></span><br><span class="line"><span class="string">docker</span> <span class="string">logs</span> <span class="string">-f</span> <span class="string">-t</span> <span class="string">--tail</span> <span class="string">日志条数</span> <span class="string">容器ID</span> <span class="comment">#查看容器日志</span></span><br><span class="line"><span class="string">docker</span> <span class="string">inspect</span> <span class="comment">#获取容器/镜像的元数据</span></span><br></pre></td></tr></table></figure>
<h2 id="二-镜像基本操作">二、镜像基本操作</h2>
<h3 id="1-拉取">1. 拉取</h3>
<figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">docker</span> <span class="string">search</span> [<span class="string">xxx</span>] <span class="comment">#查找名为xxx的镜像</span></span><br><span class="line"><span class="string">docker</span> <span class="string">pull</span> [<span class="string">xxx</span>]<span class="string">：版本号</span> <span class="comment"># 拉取xxx镜像，不加版本号则默认拉取最新版本</span></span><br></pre></td></tr></table></figure>
<h3 id="2-查看">2. 查看</h3>
<figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">docker</span> <span class="string">images</span> <span class="string">-a/-qa</span> <span class="comment">#列出本地全部镜像/全部镜像的ID</span></span><br></pre></td></tr></table></figure>
<p><img src="http://img.xiajibagao.top/docker查看镜像.PNG"></p>
<h3 id="3-删除">3. 删除</h3>
<figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">docker</span> <span class="string">rmi</span> <span class="string">-f</span> [<span class="string">xxx</span>] <span class="comment">#删除名为xxx的镜像，加-f为强制删除（不论现在正在运行）</span></span><br><span class="line"><span class="string">docker</span> <span class="string">rmi</span> <span class="string">$(docker</span> <span class="string">images</span> <span class="string">-q)</span> <span class="comment">#删除所有镜像</span></span><br></pre></td></tr></table></figure>
<p>注意：要拉取的镜像名确认不要写错，否则会报错（repository does not exist or may require 'docker login'）</p>
<h3 id="4-导出镜像">4. 导出镜像</h3>
<figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#保存镜像到指定目录</span></span><br><span class="line"><span class="comment">#这里的原image名和版本号需要与要保存的镜像一致</span></span><br><span class="line"><span class="string">docker</span> <span class="string">save</span>  <span class="string">镜像ID</span> <span class="string">-o</span> <span class="string">d:\dockerimages\新image名.tar</span> <span class="string">原image名:版本号</span></span><br></pre></td></tr></table></figure>
<h3 id="5-导入镜像">5. 导入镜像</h3>
<figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#从指定目录加载镜像</span></span><br><span class="line"><span class="string">docker</span> <span class="string">load</span> <span class="string">-i</span> <span class="string">\dockerimages\新image名.tar</span></span><br></pre></td></tr></table></figure>
<h2 id="三-容器基本操作">三、容器基本操作</h2>
<figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#-i为以交互模式启动容器</span></span><br><span class="line"><span class="comment">#-t为容器重新分配一个伪输入终端（两者合写为-it）</span></span><br><span class="line"><span class="comment">#--name为容器命名，不加则默认自动分配</span></span><br><span class="line"><span class="string">docker</span> <span class="string">run</span> <span class="string">-it</span> <span class="string">--name</span> <span class="string">mycentos</span> <span class="string">centos</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#创建守护式容器</span></span><br><span class="line"><span class="comment">#直接在后台创建一个容器，但是如果容器里没有活动则会在创建后立刻自动停止</span></span><br><span class="line"><span class="string">docker</span> <span class="string">run</span> <span class="string">-d</span> <span class="string">centos</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#8080为docker服务对外暴露的端口，8080指docker内的tomcat端口</span></span><br><span class="line"><span class="string">docker</span> <span class="string">run</span> <span class="string">-it</span> <span class="string">-p</span> <span class="number">8080</span><span class="string">:8080</span> <span class="string">tomcat</span></span><br></pre></td></tr></table></figure>
<h3 id="1-查看">1. 查看</h3>
<figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">docker</span> <span class="string">ps</span> <span class="comment">#查看当前运行的容器</span></span><br><span class="line"><span class="string">docker</span> <span class="string">ps</span> <span class="string">-l</span> <span class="comment">#列出最近创建的容器</span></span><br><span class="line"><span class="string">docker</span> <span class="string">ps</span> <span class="string">-a</span> <span class="comment">#列出当前和曾经运行的容器</span></span><br><span class="line"><span class="string">docker</span> <span class="string">ps</span> <span class="string">-n</span> <span class="string">x</span> <span class="comment">#显示最近运行过的n个容器</span></span><br><span class="line"><span class="comment">#在以上基础上变为 -xq 可以只显示容器编号</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#如果找不到命令，则可能为该镜像没有包含procps工具</span></span><br><span class="line"><span class="string">apt-get</span> <span class="string">update</span> <span class="string">&amp;&amp;</span> <span class="string">apt-get</span> <span class="string">install</span> <span class="string">procps</span></span><br></pre></td></tr></table></figure>
<p><img src="http://img.xiajibagao.top/docker查看容器.PNG"></p>
<h3 id="2-进入">2. 进入</h3>
<figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#在已运行的容器中，执行命令，操作对象是容器，如果你要进入已运行的容器，并且执行命令，用exec；</span></span><br><span class="line"><span class="string">docker</span> <span class="string">exec</span> <span class="string">-it</span> <span class="string">容器名/容器ID</span> <span class="string">/bin/bash</span></span><br><span class="line"><span class="comment">#同样操作的是已运行的容器，可以将本机标准输入（键盘输入）输到容器中，也可以将容器的输出显示在本机的屏幕上，如果你想查看容器运行过程中产生的标准输入输出，用attach；</span></span><br><span class="line"><span class="string">docker</span> <span class="string">attach</span> <span class="string">容器名/容器ID</span></span><br></pre></td></tr></table></figure>
<h3 id="3-停止">3. 停止</h3>
<figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">docker</span> <span class="string">stop</span> <span class="string">容器名/容器ID</span> <span class="comment">#停止单个</span></span><br><span class="line"><span class="string">docker</span> <span class="string">stop</span> <span class="string">$(docker</span> <span class="string">ps</span> <span class="string">-a</span> <span class="string">-q)</span> <span class="comment">#停止全部</span></span><br><span class="line"><span class="string">docker</span> <span class="string">kill</span> <span class="string">容器名/容器ID</span> <span class="comment">#强制停止</span></span><br></pre></td></tr></table></figure>
<h3 id="4-退出">4. 退出</h3>
<p>需要注意的是，容器停止后里面的数据就直接销毁了，所有如果没有提交镜像还没有备份，最好不要直接使用exit退出容器</p>
<figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">exit</span> <span class="comment">#容器停止并退出</span></span><br><span class="line"><span class="string">ctrl+P+Q</span> <span class="comment">#容器不停止退出（非常重要！！！！！！！）</span></span><br></pre></td></tr></table></figure>
<h3 id="5-删除">5. 删除</h3>
<figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">docker</span> <span class="string">rm</span> <span class="string">容器名/容器ID</span> <span class="comment">#删除单个</span></span><br><span class="line"><span class="string">docker</span> <span class="string">rm</span> <span class="string">$(docker</span> <span class="string">ps</span> <span class="string">-a</span> <span class="string">-q)</span> <span class="comment">#删除全部</span></span><br></pre></td></tr></table></figure>
<p>注意：rm为删除容器，rmi为删除镜像</p>
<h3 id="6-提交容器">6. 提交容器</h3>
<p>如果想要重复使用当前容器，可以将容器提交成镜像，那样下次使用时就可以直接拉取镜像并启动容器即可</p>
<figure class="highlight routeros"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker commit <span class="attribute">-m</span>=<span class="string">&quot;描述信息&quot;</span> <span class="attribute">-a</span>=<span class="string">&quot;作者&quot;</span> 容器ID 要创建的新镜像名:版本号</span><br></pre></td></tr></table></figure>
<h3 id="7-从容器中拷贝文件到主机中">7. 从容器中拷贝文件到主机中</h3>
<p>顾名思义，将容器中的文件拷贝到主机，如日志或者sql文件</p>
<figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">docker</span> <span class="string">cp</span> <span class="string">容器名/容器ID:文件位置</span> <span class="string">/root</span></span><br></pre></td></tr></table></figure>
<h3 id="8-docker安装mysql">8. docker安装mysql</h3>
<figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#获取镜像</span></span><br><span class="line"><span class="string">docker</span> <span class="string">pull</span> <span class="string">mysql</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#启动容器</span></span><br><span class="line"><span class="string">docker</span> <span class="string">run</span> <span class="string">-it</span> <span class="string">-p</span> <span class="number">3306</span><span class="string">:3306</span> <span class="string">--name</span> <span class="string">mymysql</span></span><br><span class="line"><span class="string">-v</span> <span class="string">/mydocker/dockersoft/mysqlfile/conf:/etc/mysql/conf.d</span> <span class="comment">#挂载容器数据卷（可不带）</span></span><br><span class="line"><span class="string">-v</span> <span class="string">/mydocker/dockersoft/mysqlfile/logs:/logs</span> <span class="comment">#挂载容器数据卷（可不带）</span></span><br><span class="line"><span class="string">-v</span> <span class="string">/mydocker/dockersoft/mysqlfile/data:/var/lib/mysql</span> <span class="comment">#挂载容器数据卷（可不带）</span></span><br><span class="line"><span class="string">-e</span> <span class="string">MYSQL_ROOT_PASSWORD=123456</span> <span class="comment">#设置默认用户名</span></span><br><span class="line"><span class="string">-d</span> <span class="string">mysql</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#进入容器</span></span><br><span class="line"><span class="string">docker</span> <span class="string">exec</span> <span class="string">-it</span> <span class="string">mymysql</span> <span class="string">/bin/bash</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#查看版本</span></span><br><span class="line"><span class="string">mysql</span> <span class="string">-v</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#以root身份登录</span></span><br><span class="line"><span class="string">mysql</span> <span class="string">-uroot</span> <span class="string">-p</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#选择mysql</span></span><br><span class="line"><span class="string">use</span> <span class="string">mysql;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#如果navicat远程连接报1521错误</span></span><br><span class="line"><span class="string">GRANT</span> <span class="string">ALL</span> <span class="string">ON</span> <span class="string">*.*</span> <span class="string">TO</span> <span class="string">&#x27;root&#x27;</span><span class="string">@&#x27;%&#x27;;</span> <span class="comment">#远程连接授权</span></span><br><span class="line"><span class="string">ALTER</span> <span class="string">USER</span> <span class="string">&#x27;root&#x27;</span><span class="string">@&#x27;%&#x27;</span> <span class="string">IDENTIFIED</span> <span class="string">BY</span> <span class="string">&#x27;password&#x27;</span> <span class="string">PASSWORD</span> <span class="string">EXPIRE</span> <span class="string">NEVER;</span> <span class="comment">#更改加密规则</span></span><br><span class="line"><span class="string">ALTER</span> <span class="string">USER</span> <span class="string">&#x27;root&#x27;</span><span class="string">@&#x27;%&#x27;</span> <span class="string">IDENTIFIED</span> <span class="string">WITH</span> <span class="string">mysql_native_password</span> <span class="string">BY</span> <span class="string">&#x27;123456&#x27;</span><span class="string">;</span> <span class="comment">#更改密码</span></span><br><span class="line"><span class="string">FLUSH</span> <span class="string">PRIVILEGES;</span> <span class="comment">#刷新权限</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h2 id="四-容器数据卷">四、容器数据卷</h2>
<p>用于数据保存和持久化,简单的理解，就是容器和主机共享的一个公共硬盘，举个例子，原本tomcat部署应用，或者查看日志还需要进入容器，现在将日志文件夹和部署文件夹拿出来挂载到主机，就可以在不进入容器的情况下查看日志或者部署项目</p>
<h3 id="1-直接构建容器数据卷">1. 直接构建容器数据卷</h3>
<figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#直接把容器内文件夹挂在到主机文件夹上</span></span><br><span class="line"><span class="string">docker</span> <span class="string">run</span> <span class="string">-it</span> <span class="string">-v</span> <span class="string">/宿主机绝对路径:/容器内目录</span> <span class="string">镜像名</span></span><br></pre></td></tr></table></figure>
<figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#在主机根目录下创建MyDockerCentos文件夹，在centos容器中创建MyDockerCentos01文件夹</span></span><br><span class="line"><span class="comment">#挂载成功后，MyDockerCentos01文件夹内容即可共享在MyDockerCentos文件夹内的内容</span></span><br><span class="line"><span class="comment">#添加ro则该文件夹为只读</span></span><br><span class="line"><span class="string">docker</span> <span class="string">run</span> <span class="string">-it</span> <span class="string">-v</span> <span class="string">/MyDockerCentos:/MyDockerCentos01</span> <span class="string">or</span> <span class="string">centos</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#通过容器元数据查看挂载情况</span></span><br><span class="line"><span class="string">docker</span> <span class="string">inspect</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#在json中可以看到此段文字</span></span><br><span class="line"><span class="string">...</span></span><br><span class="line"><span class="attr">&quot;Mounts&quot;:</span> [</span><br><span class="line">            &#123;</span><br><span class="line">                <span class="attr">&quot;Type&quot;:</span> <span class="string">&quot;bind&quot;</span>,</span><br><span class="line">                <span class="attr">&quot;Source&quot;:</span> <span class="string">&quot;/MyDockerCentos&quot;</span>, <span class="comment">#此为主机挂载文件夹路径</span></span><br><span class="line">                <span class="attr">&quot;Destination&quot;:</span> <span class="string">&quot;/MyDockerCentos01&quot;</span>, <span class="comment">#此为容器挂载文件夹路径</span></span><br><span class="line">                <span class="attr">&quot;Mode&quot;:</span> <span class="string">&quot;&quot;</span>,</span><br><span class="line">                <span class="attr">&quot;RW&quot;:</span> <span class="literal">true</span>, <span class="comment">#若为只读，则&quot;RW&quot;为false</span></span><br><span class="line">                <span class="attr">&quot;Propagation&quot;:</span> <span class="string">&quot;rprivate&quot;</span></span><br><span class="line">            &#125;</span><br><span class="line">        ]<span class="string">,</span></span><br><span class="line"><span class="string">...</span></span><br></pre></td></tr></table></figure>
<h3 id="2通过dockerfile构建容器数据卷">2.通过DockerFile构建容器数据卷</h3>
<p>关于DockerFile，具体介绍见下文</p>
<h4 id="21-编写一个构建docker镜像的脚本">2.1 编写一个构建docker镜像的脚本</h4>
<figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#在当前跟根目录新建mydockerfile</span></span><br><span class="line"><span class="string">vim</span> <span class="string">mydockerfile</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#写入以下命令</span></span><br><span class="line"><span class="string">FROM</span> <span class="string">centos</span></span><br><span class="line"><span class="string">VOLUME</span> [<span class="string">&quot;/dataVolumeContainer1&quot;</span>,<span class="string">&quot;/dataVolumeContainer2&quot;</span>]</span><br><span class="line"><span class="string">CMD</span> <span class="string">echo</span> <span class="string">&quot;create success!&quot;</span></span><br><span class="line"><span class="string">CMD</span> <span class="string">/bin/bash</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#注意：可以理解为</span></span><br><span class="line"><span class="string">docker</span> <span class="string">run</span> <span class="string">-it</span> <span class="string">-v</span> <span class="string">/host1:/dataVolumeContainer1</span> <span class="string">-v</span> <span class="string">/host2:/dataVolumeContainer1</span> <span class="string">centeros</span> <span class="string">/bin/bash</span></span><br></pre></td></tr></table></figure>
<h5 id="22-执行dockerfile">2.2 执行dockerfile</h5>
<figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#注意：命令最后有一个“ . ”，请务必带不要忘记</span></span><br><span class="line"><span class="string">docker</span> <span class="string">build</span> <span class="string">-f</span> <span class="string">/mydocker/mydockerfile</span> <span class="string">-t</span> [<span class="string">新镜像名字</span>] <span class="string">.</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#执行完后打印信息</span></span><br><span class="line">[<span class="string">root@iZwz9ev02los7q1d71e7muZ</span> <span class="string">mydocker</span>]<span class="comment"># docker build -f /mydocker/mydockerfile -t centos .</span></span><br><span class="line"><span class="string">Sending</span> <span class="string">build</span> <span class="string">context</span> <span class="string">to</span> <span class="string">Docker</span> <span class="string">daemon</span>  <span class="number">2.</span><span class="string">048kB</span></span><br><span class="line"><span class="attr">Step 1/4 :</span> <span class="string">FROM</span> <span class="string">centos</span></span><br><span class="line"> <span class="string">---&gt;</span> <span class="string">9f38484d220f</span> <span class="comment">#此为文件中指定的镜像模板ID</span></span><br><span class="line"><span class="attr">Step 2/4 :</span> <span class="string">VOLUME</span> [<span class="string">&quot;/dataVolumeContainer1&quot;</span>,<span class="string">&quot;/dataVolumeContainer2&quot;</span>]</span><br><span class="line"> <span class="string">---&gt;</span> <span class="string">Running</span> <span class="string">in</span> <span class="string">2b7657366a63</span></span><br><span class="line"><span class="string">Removing</span> <span class="string">intermediate</span> <span class="string">container</span> <span class="string">2b7657366a63</span></span><br><span class="line"> <span class="string">---&gt;</span> <span class="string">abec456be739</span></span><br><span class="line"><span class="attr">Step 3/4 :</span> <span class="string">CMD</span> <span class="string">echo</span> <span class="string">&quot;create success!&quot;</span></span><br><span class="line"> <span class="string">---&gt;</span> <span class="string">Running</span> <span class="string">in</span> <span class="string">9e043eaa69dd</span></span><br><span class="line"><span class="string">Removing</span> <span class="string">intermediate</span> <span class="string">container</span> <span class="string">9e043eaa69dd</span></span><br><span class="line"> <span class="string">---&gt;</span> <span class="string">ff5086933e91</span></span><br><span class="line"><span class="attr">Step 4/4 :</span> <span class="string">CMD</span> <span class="string">/bin/bash</span></span><br><span class="line"> <span class="string">---&gt;</span> <span class="string">Running</span> <span class="string">in</span> <span class="string">046617fa2f7a</span></span><br><span class="line"><span class="string">Removing</span> <span class="string">intermediate</span> <span class="string">container</span> <span class="string">046617fa2f7a</span></span><br><span class="line"> <span class="string">---&gt;</span> <span class="string">0245ba1fc082</span> <span class="comment">#根据此id新运行一个容器</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#新运行一个容器</span></span><br><span class="line"><span class="string">docker</span> <span class="string">run</span> <span class="string">-it</span> <span class="string">0245ba1fc082</span> <span class="string">/bin/bash</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#查看根目录所有文件后可见</span></span><br><span class="line">[<span class="string">root@2ca987ed7f70</span> <span class="string">/</span>]<span class="comment"># ll</span></span><br><span class="line"><span class="string">...</span></span><br><span class="line"><span class="string">drwxr-xr-x</span>  <span class="number">2</span> <span class="string">root</span> <span class="string">root</span>  <span class="number">4096 </span><span class="string">Jul</span> <span class="number">31</span> <span class="number">15</span><span class="string">:06</span> <span class="string">dataVolumeContainer1</span> <span class="comment">#dataVolumeContainer1</span></span><br><span class="line"><span class="string">drwxr-xr-x</span>  <span class="number">2</span> <span class="string">root</span> <span class="string">root</span>  <span class="number">4096 </span><span class="string">Jul</span> <span class="number">31</span> <span class="number">15</span><span class="string">:06</span> <span class="string">dataVolumeContainer2</span> <span class="comment">#dataVolumeContainer2</span></span><br><span class="line"><span class="string">...</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h5 id="23-查看数据卷情况">2.3 查看数据卷情况</h5>
<figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#查看该容器元信息</span></span><br><span class="line"><span class="string">docker</span> <span class="string">inspect</span> <span class="string">2ca987ed7f70</span></span><br><span class="line"><span class="comment">#可知当未指定文件夹在主机的路径时，则默认创建在/var/lib/docker/volumes目录下</span></span><br><span class="line"><span class="string">...</span></span><br><span class="line"><span class="attr">&quot;Mounts&quot;:</span> [</span><br><span class="line">            &#123;</span><br><span class="line">                <span class="attr">&quot;Type&quot;:</span> <span class="string">&quot;volume&quot;</span>,</span><br><span class="line">                <span class="attr">&quot;Name&quot;:</span> <span class="string">&quot;008c479311eb38775908647c6f89cebbed368107a418a385ee58b5461341db0b&quot;</span>,</span><br><span class="line">                <span class="attr">&quot;Source&quot;:</span> <span class="string">&quot;/var/lib/docker/volumes/008c479311eb38775908647c6f89cebbed368107a418a385ee58b5461341db0b/_data&quot;</span>,</span><br><span class="line">                <span class="attr">&quot;Destination&quot;:</span> <span class="string">&quot;/dataVolumeContainer2&quot;</span>,</span><br><span class="line">                <span class="attr">&quot;Driver&quot;:</span> <span class="string">&quot;local&quot;</span>,</span><br><span class="line">                <span class="attr">&quot;Mode&quot;:</span> <span class="string">&quot;&quot;</span>,</span><br><span class="line">                <span class="attr">&quot;RW&quot;:</span> <span class="literal">true</span>,</span><br><span class="line">                <span class="attr">&quot;Propagation&quot;:</span> <span class="string">&quot;&quot;</span></span><br><span class="line">            &#125;,</span><br><span class="line">            &#123;</span><br><span class="line">                <span class="attr">&quot;Type&quot;:</span> <span class="string">&quot;volume&quot;</span>,</span><br><span class="line">                <span class="attr">&quot;Name&quot;:</span> <span class="string">&quot;fdbf4a0b6921b7950a214f8bf028cf00edad091870e523bd694a984cc87fac30&quot;</span>,</span><br><span class="line">                <span class="attr">&quot;Source&quot;:</span> <span class="string">&quot;/var/lib/docker/volumes/fdbf4a0b6921b7950a214f8bf028cf00edad091870e523bd694a984cc87fac30/_data&quot;</span>,</span><br><span class="line">                <span class="attr">&quot;Destination&quot;:</span> <span class="string">&quot;/dataVolumeContainer1&quot;</span>,</span><br><span class="line">                <span class="attr">&quot;Driver&quot;:</span> <span class="string">&quot;local&quot;</span>,</span><br><span class="line">                <span class="attr">&quot;Mode&quot;:</span> <span class="string">&quot;&quot;</span>,</span><br><span class="line">                <span class="attr">&quot;RW&quot;:</span> <span class="literal">true</span>,</span><br><span class="line">                <span class="attr">&quot;Propagation&quot;:</span> <span class="string">&quot;&quot;</span></span><br><span class="line">            &#125;</span><br><span class="line">        ]<span class="string">,</span></span><br><span class="line"><span class="string">...</span></span><br></pre></td></tr></table></figure>
<h2 id="五-dockerfile脚本">五、DockerFile脚本</h2>
<h3 id="1-dockerfile简介">1. DockerFile简介</h3>
<p>生成docker镜像的脚本,类似于.bat批处理文件，构建容器的时候会按脚本执行构建，如果在里面写好预先写好环境配置的相关命令，那么执行完脚本以后，就能直接得到一个已经配置好环境的容器</p>
<h3 id="2-dockerfile指令">2. DockerFile指令</h3>
<figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">FROM</span> <span class="comment">#基础镜像，指定</span></span><br><span class="line"><span class="string">MAINTAINER</span> <span class="comment">#镜像维护者信息</span></span><br><span class="line"><span class="string">RUN</span> <span class="comment">#容器构建时需要运行的命令</span></span><br><span class="line"><span class="string">EXPOSE</span> <span class="comment">#当前容器对外暴露的端口</span></span><br><span class="line"><span class="string">WORKDIR</span> <span class="comment">#主机进入容器后的所在的默认路径位置</span></span><br><span class="line"><span class="string">ENV</span> <span class="comment">#用于在构建镜像的过程中设置环境变量</span></span><br><span class="line"><span class="string">COPY/ADD</span> <span class="comment">#将主机文件拷贝至镜像，若ADD则还会自动解压压缩包和出来URL</span></span><br><span class="line"><span class="string">VOLUME</span> <span class="comment">#容器数据卷，用于数据保存和持久化</span></span><br><span class="line"><span class="string">CMD</span> <span class="comment">#指定容器启动时要运行的命令（脚本中可以有多个，但是只有最后一个生效）</span></span><br><span class="line"><span class="string">ENTRYPOINT</span> <span class="comment">#同CMD，但是不会被docker run之后的参数覆盖</span></span><br><span class="line"><span class="string">ONBUILD</span> <span class="comment">#父镜像被子镜像继承后触发</span></span><br></pre></td></tr></table></figure>
<h3 id="3-dockerignore">3. dockerignore</h3>
<figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#comment</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#代表根目录（上下文环境目录中）中以abc开头的任意直接子目录或者直接子文件将被忽略</span></span><br><span class="line"><span class="comment">#如/abc  abc.txt</span></span><br><span class="line"><span class="string">/abc*</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#代表根目录（上下文环境目录中）中任意直接子目录中以abc开头的任意直接子目录或者直接子文件将被忽略</span></span><br><span class="line"><span class="comment">#如 /file/abc  /file/abc.txt</span></span><br><span class="line"><span class="string">*/abc*</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#代表根目录（上下文环境目录中）中任意两级目录下以abc开头的任意直接子目录或者直接子文件将被忽略</span></span><br><span class="line"><span class="comment">#如 /file1/file2/abc  /file1/file2/abc.txt</span></span><br><span class="line"><span class="string">*/*/abc*</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#排除根目录中的文件和目录，其名称是单字符扩展名temp。例如，/tempa与/tempb被排除在外。</span></span><br><span class="line"><span class="string">temp?</span>	</span><br><span class="line"></span><br><span class="line"><span class="comment">#Docker还支持一个**匹配任意数量目录（包括零）的特殊通配符字符串</span></span><br><span class="line"><span class="string">**/abc*</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#以!（感叹号）开头的行可用于对排除项进行例外处理,比如原本包含了README.md这个文件的过滤，但是加了如下一行后</span></span><br><span class="line"><span class="comment">#就不会再过滤README.md，依然会将其提交到守护进程。</span></span><br><span class="line"><span class="type">!README.md</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#异常规则的放置位置会影响行为</span></span><br><span class="line"><span class="string">*.md</span></span><br><span class="line"><span class="type">!README*.md</span></span><br><span class="line"><span class="string">README-secret.md</span></span><br><span class="line"><span class="comment">#README-secret.md 仍然会被忽略</span></span><br><span class="line">	</span><br><span class="line"><span class="string">*.md</span></span><br><span class="line"><span class="string">README-secret.md</span></span><br><span class="line"><span class="type">!README*.md</span></span><br><span class="line"><span class="comment">#README-secret.md 不会被忽略</span></span><br><span class="line"></span><br><span class="line"><span class="string">您甚至可以使用该.dockerignore文件来排除Dockerfile和.dockerignore文件。这些文件仍然发送到守护程序，因为它需要它们来完成它的工作。但是ADD和COPY命令不会将它们复制到图像中。</span></span><br></pre></td></tr></table></figure>
<h2 id="六-dockerfile实战">六、DockerFile实战</h2>
<h3 id="1使用dockerfile自定义一个centos镜像">1.使用DockerFile自定义一个centos镜像</h3>
<p><strong>第一步，编写脚本</strong>：</p>
<figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">FORM</span> <span class="string">centos</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#ENV mypath /temp #设置一个环境变量</span></span><br><span class="line"><span class="comment">#WORKDIR $mypath #设置默认进入容器的路径，指向环境变量所规定的路径</span></span><br><span class="line"></span><br><span class="line"><span class="string">RUN</span> <span class="string">yum</span> <span class="string">install</span> <span class="string">vim</span> <span class="string">-y</span> <span class="comment">#安装vim</span></span><br><span class="line"><span class="string">RUN</span> <span class="string">yum</span> <span class="string">install</span> <span class="string">tree</span> <span class="string">-y</span> <span class="comment">#安装tree</span></span><br><span class="line"></span><br><span class="line"><span class="string">EXPOSE</span> <span class="number">80</span> <span class="comment">#暴露出80端口</span></span><br><span class="line"></span><br><span class="line"><span class="string">CMD</span> <span class="string">echo</span> <span class="string">&quot;success!&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="string">CMD</span> <span class="string">/bin/bash</span></span><br></pre></td></tr></table></figure>
<p><strong>第二步，运行脚本</strong>：</p>
<figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">docker</span> <span class="string">build</span> <span class="string">-f</span> <span class="string">/mydocker/dockerfile/mydockerfile2</span> <span class="string">-t</span> <span class="string">mycentos</span> <span class="string">.</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#使用命令查看容器历史可见</span></span><br><span class="line"><span class="string">docker</span> <span class="string">history</span> <span class="string">22d8bbae3a6f</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#顺序加载，倒序执行</span></span><br><span class="line"><span class="string">IMAGE</span>               <span class="string">CREATED</span>             <span class="string">CREATED</span> <span class="string">BY</span>                              		<span class="string">SIZE</span>   </span><br><span class="line"><span class="string">22d8bbae3a6f</span>        <span class="number">3</span> <span class="string">hours</span> <span class="string">ago</span>         <span class="string">/bin/sh</span> <span class="string">-c</span> <span class="string">yum</span> <span class="string">install</span> <span class="string">tree</span> <span class="string">-y</span> <span class="comment">#安装tree          23MB         </span></span><br><span class="line"><span class="string">b77e4249fba5</span>        <span class="number">3</span> <span class="string">hours</span> <span class="string">ago</span>         <span class="string">/bin/sh</span> <span class="string">-c</span> <span class="string">yum</span> <span class="string">install</span> <span class="string">vim</span> <span class="string">-y</span> <span class="comment">#安装vim            167MB       </span></span><br><span class="line"><span class="string">9f38484d220f</span>        <span class="number">4</span> <span class="string">months</span> <span class="string">ago</span>        <span class="string">/bin/sh</span> <span class="string">-c</span> <span class="comment">#(nop)  CMD [&quot;/bin/bash&quot;]              0B         </span></span><br><span class="line"><span class="string">&lt;missing&gt;</span>           <span class="number">4</span> <span class="string">months</span> <span class="string">ago</span>        <span class="string">/bin/sh</span> <span class="string">-c</span> <span class="comment">#(nop)  LABEL org.label-schema.sc…     0B         </span></span><br><span class="line"><span class="string">&lt;missing&gt;</span>           <span class="number">4</span> <span class="string">months</span> <span class="string">ago</span>        <span class="string">/bin/sh</span> <span class="string">-c</span> <span class="comment">#(nop) ADD file:074f2c974463ab38c…     202MB </span></span><br></pre></td></tr></table></figure>
<p>注意：如果无法解决权限问题报错，可以进入dockerfile文件夹后运行程序</p>
<h3 id="2自定义一个带jdk和tomcat的centos">2.自定义一个带jdk和tomcat的centos</h3>
<p><strong>第一步，编写脚本</strong>：</p>
<figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">FROM</span> <span class="string">centos</span></span><br><span class="line"><span class="string">MAINTAINER</span> <span class="string">huang&lt;841396397@qq.com&gt;</span></span><br><span class="line"><span class="comment">#把主机下的目录tomcat和jdk添加至容器，这里脚本与安装包在同一路径下</span></span><br><span class="line"><span class="string">ADD</span> <span class="string">apache-tomcat-8.5.43.tar.gz</span> <span class="string">/usr/local</span></span><br><span class="line"><span class="string">ADD</span> <span class="string">jdk-8u221-linux-i586.tar.gz</span> <span class="string">/usr/local</span></span><br><span class="line"><span class="comment">#安装插件</span></span><br><span class="line"><span class="string">RUN</span> <span class="string">yum</span> <span class="string">install</span> <span class="string">vim</span> <span class="string">-y</span></span><br><span class="line"><span class="string">RUN</span> <span class="string">yum</span> <span class="string">install</span> <span class="string">wget</span> <span class="string">-y</span> </span><br><span class="line"><span class="string">RUN</span> <span class="string">yum</span> <span class="string">install</span> <span class="string">tree</span> <span class="string">-y</span></span><br><span class="line"><span class="string">RUN</span> <span class="string">yum</span> <span class="string">install</span> <span class="string">bash-completion</span> <span class="string">-y</span></span><br><span class="line"><span class="comment">#设置访问时的容器路径</span></span><br><span class="line"><span class="string">ENV</span> <span class="string">MYPATH</span> <span class="string">/usr/local</span></span><br><span class="line"><span class="string">WORKDIR</span> <span class="string">$MYPATH</span></span><br><span class="line"><span class="comment">#配置JDK和tomcat环境 </span></span><br><span class="line"><span class="string">ENV</span> <span class="string">JAVA_HOME</span> <span class="string">/usr/local/jdk1.8.0_221</span></span><br><span class="line"><span class="string">ENV</span> <span class="string">CLASSPATH</span> <span class="string">$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar</span></span><br><span class="line"><span class="string">ENV</span> <span class="string">CATALINA_HOME</span> <span class="string">/usr/local/apache-tomcat-8.5.43</span></span><br><span class="line"><span class="string">ENV</span> <span class="string">CATALINA_BASE</span> <span class="string">/usr/local/apache-tomcat-8.5.43</span></span><br><span class="line"><span class="string">ENV</span> <span class="string">PATH</span> <span class="string">$PATH:$JAVA_HOME/lib:$CATALINA_HOME/lib:$$CATALINA_HOME/bin</span></span><br><span class="line"><span class="comment">#容器运行时监听的端口</span></span><br><span class="line"><span class="string">EXPOSE</span> <span class="number">8080</span></span><br><span class="line"><span class="comment">#启动时运行tomcat</span></span><br><span class="line"><span class="string">CMD</span> <span class="string">/usr/local/apache-tomcat-8.5.43/bin/startup.sh</span> <span class="string">&amp;&amp;</span> <span class="string">tail</span> <span class="string">-F</span> <span class="string">/usr/local/apache-tomcat-8.5.43/bin/logs/catalina.out</span></span><br></pre></td></tr></table></figure>
<p><strong>第二步，运行脚本</strong>：</p>
<figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">docker</span> <span class="string">build</span> <span class="string">-f</span> <span class="string">/mydocker/dockerfile/tomcat</span> <span class="string">-t</span> <span class="string">mytomcat</span> <span class="string">.</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#运行成功</span></span><br><span class="line"><span class="string">REPOSITORY</span>          <span class="string">TAG</span>                 <span class="string">IMAGE</span> <span class="string">ID</span>            <span class="string">CREATED</span>             <span class="string">SIZE</span></span><br><span class="line"><span class="string">mytomcat</span>            <span class="string">latest</span>              <span class="string">71e04bed14cd</span>        <span class="number">9</span> <span class="string">seconds</span> <span class="string">ago</span>       <span class="string">859MB</span></span><br></pre></td></tr></table></figure>
<p><strong>第三步，启动容器</strong>：</p>
<p>注意：配置tomcat和jdk参考 ” linux常用操作 “</p>
<figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#容器暴露端口，名称</span></span><br><span class="line"><span class="string">docker</span> <span class="string">run</span> <span class="string">-it</span> <span class="string">-p</span> <span class="number">8001</span><span class="string">:8080</span> <span class="string">--name</span> <span class="string">mycat</span></span><br><span class="line"><span class="comment">#创建两个容器数据卷</span></span><br><span class="line"><span class="string">-v</span> <span class="string">/mydocker/dockersoft/tomcatfile/webapp:/usr/local/apache-tomcat-8.5.43/webapp</span></span><br><span class="line"><span class="string">-v</span> <span class="string">/mydocker/dockersoft/tomcatfile/logs:/usr/local/apache-tomcat-8.5.43/logs</span></span><br><span class="line"><span class="string">--privileged=true</span></span><br><span class="line"><span class="comment">#镜像模板</span></span><br><span class="line"><span class="string">mytomcat</span></span><br></pre></td></tr></table></figure>
<figure class="highlight cmd"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">#容器启动</span><br><span class="line">CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS           </span><br><span class="line">edd612a7f92f        mytomcat            &quot;/bin/sh -c &#x27;/usr/lo…&quot;   <span class="number">24</span> seconds ago      Up <span class="number">23</span> seconds  </span><br><span class="line"></span><br><span class="line">PORTS                    NAMES</span><br><span class="line"><span class="number">0</span>.<span class="number">0</span>.<span class="number">0</span>.<span class="number">0</span>:<span class="number">8001</span>-&gt;<span class="number">8080</span>/tcp   mycat</span><br></pre></td></tr></table></figure>
<h2 id="七-使用阿里云镜像仓库管理镜像">七、使用阿里云镜像仓库管理镜像</h2>
<p>选中镜像仓库管理首页的镜像仓库选项，根据提示创建命名空间和镜像仓库，并绑定github或其他代码托管平台账号</p>
<h3 id="1-打包镜像">1. 打包镜像</h3>
<figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#把成功运行的容器打包成镜像</span></span><br><span class="line"><span class="string">docker</span> <span class="string">commit</span> <span class="string">-m=&quot;jdk+tomcat+centos&quot;</span> <span class="string">-a=&quot;huang&quot;</span> <span class="string">c0733d9e5807</span> <span class="string">mycentos:1.0</span></span><br><span class="line"><span class="comment">#查看镜像</span></span><br><span class="line"><span class="string">Docker</span> <span class="string">Registry</span></span><br><span class="line"><span class="comment">#可见新镜像</span></span><br><span class="line"><span class="string">REPOSITORY</span>          <span class="string">TAG</span>                 <span class="string">IMAGE</span> <span class="string">ID</span>            <span class="string">CREATED</span>             <span class="string">SIZE</span></span><br><span class="line"><span class="string">mycentos</span>            <span class="number">1.0</span>                 <span class="string">fe6434ec860e</span>        <span class="number">6</span> <span class="string">seconds</span> <span class="string">ago</span>       <span class="string">923MB</span></span><br></pre></td></tr></table></figure>
<h3 id="2-登录阿里云docker-registry">2. 登录阿里云Docker Registry</h3>
<figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#用于登录的用户名为阿里云账号全名，密码为开通服务时设置的密码</span></span><br><span class="line"><span class="string">sudo</span> <span class="string">docker</span> <span class="string">login</span> <span class="string">--username=xxxxxxxx</span> <span class="string">registry.cn-shenzhen.aliyuncs.com</span></span><br><span class="line"><span class="string">xxxpasswordxxx</span></span><br></pre></td></tr></table></figure>
<h3 id="3-从registry拉取推送镜像">3. 从Registry拉取/推送镜像</h3>
<figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#拉取镜像</span></span><br><span class="line"><span class="string">sudo</span> <span class="string">docker</span> <span class="string">pull</span> <span class="string">registry.cn-shenzhen.aliyuncs.com/xxxx/depository01:[镜像版本号]</span></span><br></pre></td></tr></table></figure>
<figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#推送镜像</span></span><br><span class="line"><span class="string">sudo</span> <span class="string">docker</span> <span class="string">tag</span> [<span class="string">ImageId</span>] <span class="string">registry.cn-shenzhen.aliyuncs.com/xxxxxx/depository01:[镜像版本号]</span></span><br><span class="line"><span class="string">sudo</span> <span class="string">docker</span> <span class="string">push</span> <span class="string">registry.cn-shenzhen.aliyuncs.com/xxxxxx/depository01:[镜像版本号]</span></span><br></pre></td></tr></table></figure>
<h3 id="4-在阿里云查看镜像">4. 在阿里云查看镜像</h3>
<figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#查看公有镜像</span></span><br><span class="line"><span class="string">进入阿里云镜像容器管理主界面，选择镜像中心的镜像搜索选项，搜索仓库</span></span><br><span class="line"><span class="string">eg：xxxxxx/depository01</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#查看私有镜像</span></span><br><span class="line"><span class="string">进入阿里云镜像容器管理主界面，选中自己的仓库，点击镜像版本管理</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/docker/" rel="tag"># docker</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2019/08/28/docker/Docker%E5%AE%89%E8%A3%85/" rel="next" title="Docker安装">
                <i class="fa fa-chevron-left"></i> Docker安装
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2019/12/01/java/JDK%E6%96%B0%E7%89%B9%E6%80%A7/JDK1.8%E6%96%B0%E7%89%B9%E6%80%A7%EF%BC%88%E4%BA%8C%EF%BC%89%EF%BC%9ACollectors%E6%94%B6%E9%9B%86%E5%99%A8%E7%B1%BB/" rel="prev" title="JDK1.8新特性（二）：Collectors收集器类">
                JDK1.8新特性（二）：Collectors收集器类 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/images/Createsequence.jpg"
                alt="" />
            
              <p class="site-author-name" itemprop="name"></p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/%7C%7Carchive">
              
                  <span class="site-state-item-count">89</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">15</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">21</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          
            <div class="feed-link motion-element">
              <a href="/atom.xml" rel="alternate">
                <i class="fa fa-rss"></i>
                RSS
              </a>
            </div>
          

          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%B8%80-docker%E5%B8%B8%E7%94%A8%E5%91%BD%E4%BB%A4"><span class="nav-text">一、Docker常用命令</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BA%8C-%E9%95%9C%E5%83%8F%E5%9F%BA%E6%9C%AC%E6%93%8D%E4%BD%9C"><span class="nav-text">二、镜像基本操作</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-%E6%8B%89%E5%8F%96"><span class="nav-text">1. 拉取</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-%E6%9F%A5%E7%9C%8B"><span class="nav-text">2. 查看</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-%E5%88%A0%E9%99%A4"><span class="nav-text">3. 删除</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-%E5%AF%BC%E5%87%BA%E9%95%9C%E5%83%8F"><span class="nav-text">4. 导出镜像</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#5-%E5%AF%BC%E5%85%A5%E9%95%9C%E5%83%8F"><span class="nav-text">5. 导入镜像</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%B8%89-%E5%AE%B9%E5%99%A8%E5%9F%BA%E6%9C%AC%E6%93%8D%E4%BD%9C"><span class="nav-text">三、容器基本操作</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-%E6%9F%A5%E7%9C%8B"><span class="nav-text">1. 查看</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-%E8%BF%9B%E5%85%A5"><span class="nav-text">2. 进入</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-%E5%81%9C%E6%AD%A2"><span class="nav-text">3. 停止</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-%E9%80%80%E5%87%BA"><span class="nav-text">4. 退出</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#5-%E5%88%A0%E9%99%A4"><span class="nav-text">5. 删除</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#6-%E6%8F%90%E4%BA%A4%E5%AE%B9%E5%99%A8"><span class="nav-text">6. 提交容器</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#7-%E4%BB%8E%E5%AE%B9%E5%99%A8%E4%B8%AD%E6%8B%B7%E8%B4%9D%E6%96%87%E4%BB%B6%E5%88%B0%E4%B8%BB%E6%9C%BA%E4%B8%AD"><span class="nav-text">7. 从容器中拷贝文件到主机中</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#8-docker%E5%AE%89%E8%A3%85mysql"><span class="nav-text">8. docker安装mysql</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%9B%9B-%E5%AE%B9%E5%99%A8%E6%95%B0%E6%8D%AE%E5%8D%B7"><span class="nav-text">四、容器数据卷</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-%E7%9B%B4%E6%8E%A5%E6%9E%84%E5%BB%BA%E5%AE%B9%E5%99%A8%E6%95%B0%E6%8D%AE%E5%8D%B7"><span class="nav-text">1. 直接构建容器数据卷</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2%E9%80%9A%E8%BF%87dockerfile%E6%9E%84%E5%BB%BA%E5%AE%B9%E5%99%A8%E6%95%B0%E6%8D%AE%E5%8D%B7"><span class="nav-text">2.通过DockerFile构建容器数据卷</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#21-%E7%BC%96%E5%86%99%E4%B8%80%E4%B8%AA%E6%9E%84%E5%BB%BAdocker%E9%95%9C%E5%83%8F%E7%9A%84%E8%84%9A%E6%9C%AC"><span class="nav-text">2.1 编写一个构建docker镜像的脚本</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#22-%E6%89%A7%E8%A1%8Cdockerfile"><span class="nav-text">2.2 执行dockerfile</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#23-%E6%9F%A5%E7%9C%8B%E6%95%B0%E6%8D%AE%E5%8D%B7%E6%83%85%E5%86%B5"><span class="nav-text">2.3 查看数据卷情况</span></a></li></ol></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BA%94-dockerfile%E8%84%9A%E6%9C%AC"><span class="nav-text">五、DockerFile脚本</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-dockerfile%E7%AE%80%E4%BB%8B"><span class="nav-text">1. DockerFile简介</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-dockerfile%E6%8C%87%E4%BB%A4"><span class="nav-text">2. DockerFile指令</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-dockerignore"><span class="nav-text">3. dockerignore</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%85%AD-dockerfile%E5%AE%9E%E6%88%98"><span class="nav-text">六、DockerFile实战</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1%E4%BD%BF%E7%94%A8dockerfile%E8%87%AA%E5%AE%9A%E4%B9%89%E4%B8%80%E4%B8%AAcentos%E9%95%9C%E5%83%8F"><span class="nav-text">1.使用DockerFile自定义一个centos镜像</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2%E8%87%AA%E5%AE%9A%E4%B9%89%E4%B8%80%E4%B8%AA%E5%B8%A6jdk%E5%92%8Ctomcat%E7%9A%84centos"><span class="nav-text">2.自定义一个带jdk和tomcat的centos</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%B8%83-%E4%BD%BF%E7%94%A8%E9%98%BF%E9%87%8C%E4%BA%91%E9%95%9C%E5%83%8F%E4%BB%93%E5%BA%93%E7%AE%A1%E7%90%86%E9%95%9C%E5%83%8F"><span class="nav-text">七、使用阿里云镜像仓库管理镜像</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-%E6%89%93%E5%8C%85%E9%95%9C%E5%83%8F"><span class="nav-text">1. 打包镜像</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-%E7%99%BB%E5%BD%95%E9%98%BF%E9%87%8C%E4%BA%91docker-registry"><span class="nav-text">2. 登录阿里云Docker Registry</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-%E4%BB%8Eregistry%E6%8B%89%E5%8F%96%E6%8E%A8%E9%80%81%E9%95%9C%E5%83%8F"><span class="nav-text">3. 从Registry拉取&#x2F;推送镜像</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-%E5%9C%A8%E9%98%BF%E9%87%8C%E4%BA%91%E6%9F%A5%E7%9C%8B%E9%95%9C%E5%83%8F"><span class="nav-text">4. 在阿里云查看镜像</span></a></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Createsequence</span>

  
</div>






  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Gemini</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
          <span id="scrollpercent"><span>0</span>%</span>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  

  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    var isXml = true;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length === 0) {
      search_path = "search.xml";
    } else if (/json$/i.test(search_path)) {
      isXml = false;
    }
    var path = "/" + search_path;
    // monitor main search box;

    var onPopupClose = function (e) {
      $('.popup').hide();
      $('#local-search-input').val('');
      $('.search-result-list').remove();
      $('#no-result').remove();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    }

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.search-popup-overlay').click(onPopupClose);
      $('.popup').toggle();
      var $localSearchInput = $('#local-search-input');
      $localSearchInput.attr("autocapitalize", "none");
      $localSearchInput.attr("autocorrect", "off");
      $localSearchInput.focus();
    }

    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';

      // start loading animation
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay">' +
          '<div id="search-loading-icon">' +
          '<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
          '</div>' +
          '</div>')
        .css('overflow', 'hidden');
      $("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');

      $.ajax({
        url: path,
        dataType: isXml ? "xml" : "json",
        async: true,
        success: function(res) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = isXml ? $("entry", res).map(function() {
            return {
              title: $("title", this).text(),
              content: $("content",this).text(),
              url: $("url" , this).text()
            };
          }).get() : res;
          var input = document.getElementById(search_id);
          var resultContent = document.getElementById(content_id);
          var inputEventFunction = function() {
            var searchText = input.value.trim().toLowerCase();
            var keywords = searchText.split(/[\s\-]+/);
            if (keywords.length > 1) {
              keywords.push(searchText);
            }
            var resultItems = [];
            if (searchText.length > 0) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var hitCount = 0;
                var searchTextCount = 0;
                var title = data.title.trim();
                var titleInLowerCase = title.toLowerCase();
                var content = data.content.trim().replace(/<[^>]+>/g,"");
                var contentInLowerCase = content.toLowerCase();
                var articleUrl = decodeURIComponent(data.url);
                var indexOfTitle = [];
                var indexOfContent = [];
                // only match articles with not empty titles
                if(title != '') {
                  keywords.forEach(function(keyword) {
                    function getIndexByWord(word, text, caseSensitive) {
                      var wordLen = word.length;
                      if (wordLen === 0) {
                        return [];
                      }
                      var startPosition = 0, position = [], index = [];
                      if (!caseSensitive) {
                        text = text.toLowerCase();
                        word = word.toLowerCase();
                      }
                      while ((position = text.indexOf(word, startPosition)) > -1) {
                        index.push({position: position, word: word});
                        startPosition = position + wordLen;
                      }
                      return index;
                    }

                    indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                    indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
                  });
                  if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                    isMatch = true;
                    hitCount = indexOfTitle.length + indexOfContent.length;
                  }
                }

                // show search results

                if (isMatch) {
                  // sort index by position of keyword

                  [indexOfTitle, indexOfContent].forEach(function (index) {
                    index.sort(function (itemLeft, itemRight) {
                      if (itemRight.position !== itemLeft.position) {
                        return itemRight.position - itemLeft.position;
                      } else {
                        return itemLeft.word.length - itemRight.word.length;
                      }
                    });
                  });

                  // merge hits into slices

                  function mergeIntoSlice(text, start, end, index) {
                    var item = index[index.length - 1];
                    var position = item.position;
                    var word = item.word;
                    var hits = [];
                    var searchTextCountInSlice = 0;
                    while (position + word.length <= end && index.length != 0) {
                      if (word === searchText) {
                        searchTextCountInSlice++;
                      }
                      hits.push({position: position, length: word.length});
                      var wordEnd = position + word.length;

                      // move to next position of hit

                      index.pop();
                      while (index.length != 0) {
                        item = index[index.length - 1];
                        position = item.position;
                        word = item.word;
                        if (wordEnd > position) {
                          index.pop();
                        } else {
                          break;
                        }
                      }
                    }
                    searchTextCount += searchTextCountInSlice;
                    return {
                      hits: hits,
                      start: start,
                      end: end,
                      searchTextCount: searchTextCountInSlice
                    };
                  }

                  var slicesOfTitle = [];
                  if (indexOfTitle.length != 0) {
                    slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                  }

                  var slicesOfContent = [];
                  while (indexOfContent.length != 0) {
                    var item = indexOfContent[indexOfContent.length - 1];
                    var position = item.position;
                    var word = item.word;
                    // cut out 100 characters
                    var start = position - 20;
                    var end = position + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if (end < position + word.length) {
                      end = position + word.length;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                  }

                  // sort slices in content by search text's count and hits' count

                  slicesOfContent.sort(function (sliceLeft, sliceRight) {
                    if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                      return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                    } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                      return sliceRight.hits.length - sliceLeft.hits.length;
                    } else {
                      return sliceLeft.start - sliceRight.start;
                    }
                  });

                  // select top N slices in content

                  var upperBound = parseInt('1');
                  if (upperBound >= 0) {
                    slicesOfContent = slicesOfContent.slice(0, upperBound);
                  }

                  // highlight title and content

                  function highlightKeyword(text, slice) {
                    var result = '';
                    var prevEnd = slice.start;
                    slice.hits.forEach(function (hit) {
                      result += text.substring(prevEnd, hit.position);
                      var end = hit.position + hit.length;
                      result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
                      prevEnd = end;
                    });
                    result += text.substring(prevEnd, slice.end);
                    return result;
                  }

                  var resultItem = '';

                  if (slicesOfTitle.length != 0) {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
                  } else {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
                  }

                  slicesOfContent.forEach(function (slice) {
                    resultItem += "<a href='" + articleUrl + "'>" +
                      "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                      "...</p>" + "</a>";
                  });

                  resultItem += "</li>";
                  resultItems.push({
                    item: resultItem,
                    searchTextCount: searchTextCount,
                    hitCount: hitCount,
                    id: resultItems.length
                  });
                }
              })
            };
            if (keywords.length === 1 && keywords[0] === "") {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>'
            } else if (resultItems.length === 0) {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>'
            } else {
              resultItems.sort(function (resultLeft, resultRight) {
                if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                  return resultRight.searchTextCount - resultLeft.searchTextCount;
                } else if (resultLeft.hitCount !== resultRight.hitCount) {
                  return resultRight.hitCount - resultLeft.hitCount;
                } else {
                  return resultRight.id - resultLeft.id;
                }
              });
              var searchResultList = '<ul class=\"search-result-list\">';
              resultItems.forEach(function (result) {
                searchResultList += result.item;
              })
              searchResultList += "</ul>";
              resultContent.innerHTML = searchResultList;
            }
          }

          if ('auto' === 'auto') {
            input.addEventListener('input', inputEventFunction);
          } else {
            $('.search-icon').click(inputEventFunction);
            input.addEventListener('keypress', function (event) {
              if (event.keyCode === 13) {
                inputEventFunction();
              }
            });
          }

          // remove loading animation
          $(".local-search-pop-overlay").remove();
          $('body').css('overflow', '');

          proceedsearch();
        }
      });
    }

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched === false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(onPopupClose);
    $('.popup').click(function(e){
      e.stopPropagation();
    });
    $(document).on('keyup', function (event) {
      var shouldDismissSearchPopup = event.which === 27 &&
        $('.search-popup').is(':visible');
      if (shouldDismissSearchPopup) {
        onPopupClose();
      }
    });
  </script>





  

  

  

  
  

  

  

  


  <!-- 引入目录截取js -->
  <script type="text/javascript" src="/js/src/custom/custom.js"></script>
</body>
</html>
